package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增分类")
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        categoryService.insertCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 分页查询菜品分类
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询分类")
    public Result page(CategoryPageQueryDTO dto) {
        PageResult pageResult = categoryService.selectCategoryList(dto);
        return Result.success(pageResult);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除分类")
    public Result delete(long id) {
        categoryService.removeById(id);
        return Result.success();
    }

    /**
     * 更新分类信息
     *
     * @param category
     * @return
     */
    @PutMapping
    @ApiOperation(value = "更新分类")
    public Result update(@RequestBody Category category) {
        categoryMapper.updateById(category);
        return Result.success();
    }

    /**
     * 查询分类回显
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询分类回显")
    public Result listAll(@RequestParam("type") Integer type) {
        List<Category> category = categoryService.lambdaQuery().eq(Category::getType, type).list();
        return Result.success(category);
    }
}